<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="申请时间" style="height: 32px;">
              <a-range-picker
                :allowClear="false"
                v-model="createTime"
                style="width: 100%"
                @change="createTimeChange"/>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-model-item label="物料大类" style="height: 32px;">
              <a-select
                v-model="queryParam.provisionBroad"
                placeholder="请选择物料大类"
                :showSearch="true"
                :allowClear="true"
                :filterOption="filterOption"
                @change="handleChange"
                :getPopupContainer="(triggerNode)=>{ return triggerNode.parentNode}"
              >
                <a-select-option v-for="item in provisionBroadList" :key="item.id" :value="item.id">{{item.categoryName}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          @change="provisionSmallChange"-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-model-item label="物料小类">
              <a-select
                v-model="queryParam.provisionSmall"
                placeholder="请选择物料小类"
                :showSearch="true"
                :allowClear="true"
                :filterOption="filterOption"
                @change="smallChange"
                :getPopupContainer="triggerNode => {return triggerNode.parentNode}"
              >
                <a-select-option v-for="item in provisionSmallList" :key="item.id" :value="item.id">{{item.categoryName}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <select-material-input v-model="queryParam.materialName"  :materialFlag="true" :materialSmall="queryParam.provisionSmall" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="申请人">
              <select-user-by-dep :multi="true" v-model="queryParam.proposer"></select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="费用所属团队">
              <select-team-input v-model="queryParam.teamId" :tdlx="3" :teamType="0" multi/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('物品领用信息统计表')" :disabled="isDisabledAuth('erpGoodsUsedInfoReport:export')">导出</a-button>
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:'180%'}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="handleTableChange">
        <a slot="purchaseBill" slot-scope="text, record" @click="getDetail(record.billNo)">{{ text }}</a>
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>


  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectMaterialInput from '@/views/scy/components/SelectMaterialInput'
  import { getAction } from '@/api/manage'
  import moment from 'moment';

  export default {
    name: 'ErpGoodsUsedInfoReportList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      SelectTeamInput,
      SelectUserByDep,
      SelectMaterialInput
    },
    data () {
      return {
        description: '物品领用信息统计表管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'billNo_dictText',
            scopedSlots: { customRender: 'purchaseBill' },
            width: 150,
            ellipsis: true,
          },
          {
            title:'申请人',
            align:"center",
            dataIndex: 'proposer_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'申请部门',
            align:"center",
            dataIndex: 'departId_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'申请时间',
            align:"center",
            dataIndex: 'createTime',
            width: 150,
            ellipsis: true,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,16):text)
            }
          },
          {
            title:'费用所属团队',
            align:"center",
            dataIndex: 'teamId_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'物料大类',
            align:"center",
            dataIndex: 'provisionBroad_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'物料小类',
            align:"center",
            dataIndex: 'provisionSmall_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialBillId',
            width: 150,
            ellipsis: true,
          },
          {
            title:'物料名称',
            align:"center",
            dataIndex: 'materialName_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'品牌',
            align:"center",
            dataIndex: 'brand',
            width: 150,
            ellipsis: true,
          },
          {
            title:'规格型号',
            align:"center",
            dataIndex: 'specification',
            width: 150,
            ellipsis: true,
          },
          {
            title:'供应商',
            align:"center",
            dataIndex: 'companName_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'unit_dictText',
            width: 150,
            ellipsis: true,
          },
          {
            title:'领用数量',
            align:"center",
            dataIndex: 'recipientsNum',
            width: 150,
            ellipsis: true,
          },
          {
            title:'物料用途',
            align:"center",
            dataIndex: 'remark',
            width: 150,
            ellipsis: true,
          },
          {
            title:'单价（元）',
            align:"center",
            dataIndex: 'referencePrice',
            width: 150,
            ellipsis: true,
          },
          {
            title:'金额（元）',
            align:"center",
            dataIndex: 'totalMoney',
            width: 150,
            ellipsis: true,
          }
        ],
        url: {
          list: "/api/erp/modules/workbench/report/erpGoodsUsedInfoReport/list",
          exportXlsUrl: "/api/erp/modules/workbench/report/erpGoodsUsedInfoReport/exportXls",
          queryProvisionBroadList: "/api/erp/purchase/material/erpMaterialCategory/queryAllParent",
          queryProvisionSmallList: "/api/erp/purchase/material/erpMaterialCategory/queryChildrenByParentId"
        },
        isorter: {
          column: 'create_time',
          order: 'desc'
        },
        provisionBroadList: [],
        provisionSmallList: [],
        queryParam: {
          createStartTime: moment().startOf('month').format("YYYY-MM-DD"),
          createEndTime: moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD')
        },
        // createTime: [moment().startOf('month').format("YYYY-MM-DD"),moment(t, dateFormat).add(1, 'months').startOf('month').format('YYYY-MM-DD')],
        createTime: [moment().startOf('month').format("YYYY-MM-DD"),moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD')],
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
      this.findProvisionBroadList()
      this.provisionSmallList = []
      if (!!this.queryParam.provisionBroad) {
        this.findProvisionSmallList(this.queryParam.provisionBroad)
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      //查看详情
      getDetail(billNo) {
        this.$router.push({
          path: `/erp/purchase/recipients/ErpPurchaseRecipientsListDetail/${billNo}`,
        })
      },
      customRow(record){},//禁用双击详情
      createTimeChange(date, dateString) {
        this.$set(this.queryParam, 'createStartTime', dateString[0] + ' 00:00:00')
        this.$set(this.queryParam, 'createEndTime', dateString[1] + ' 23:59:59')
      },
      searchReset() {
        this.queryParam = {
          createStartTime: moment().startOf('month').format("YYYY-MM-DD"),
          createEndTime: moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD')
        }
        this.createTime = [moment().startOf('month').format("YYYY-MM-DD"),moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD')]
        console.log('传递的时间参数为：',this.createTime)
        this.loadData(1)
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      handleChange(value) {
        this.provisionSmallList = []
        this.$set(this.queryParam, 'provisionSmall', undefined);
        this.$set(this.queryParam,'materialName',undefined);
        if (!!value) {
          this.findProvisionSmallList(value)
        }
      },
      smallChange(value){
        this.$set(this.queryParam,'materialName',undefined);
      },
      findProvisionBroadList() {
        const that = this
        that.provisionBroadList = []
        getAction(this.url.queryProvisionBroadList).then(res => {
          if (res.success) {
            res.result.forEach((res) => {
              that.provisionBroadList.push({ 'id':res.id, 'categoryName':res.categoryName })
            })
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      findProvisionSmallList(value) {
        const that = this
        const parentId = value
        getAction(this.url.queryProvisionSmallList, { parentId }).then(res => {
          if (res.success) {
            res.result.forEach((res) => {
              that.provisionSmallList.push({ 'id':res.id, 'categoryName':res.categoryName })
            })
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'billNo',text:'单据编号',dictCode:''})
        fieldList.push({type:'string',value:'proposer',text:'申请人',dictCode:''})
        fieldList.push({type:'string',value:'departId',text:'申请部门',dictCode:''})
        fieldList.push({type:'date',value:'createTime',text:'申请时间'})
        fieldList.push({type:'string',value:'teamId',text:'费用所属团队',dictCode:''})
        fieldList.push({type:'string',value:'provisionBroad',text:'物料大类',dictCode:''})
        fieldList.push({type:'string',value:'provisionSmall',text:'物料小类',dictCode:''})
        fieldList.push({type:'string',value:'materialBillId',text:'物料编号',dictCode:''})
        fieldList.push({type:'string',value:'materialName',text:'物料名称',dictCode:''})
        fieldList.push({type:'string',value:'brand',text:'品牌',dictCode:''})
        fieldList.push({type:'string',value:'specification',text:'规格型号',dictCode:''})
        fieldList.push({type:'string',value:'companName',text:'供应商',dictCode:''})
        fieldList.push({type:'string',value:'unit',text:'单位',dictCode:''})
        fieldList.push({type:'string',value:'recipientsNum',text:'领用数量',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'物料用途',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'referencePrice',text:'单价（元）',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'totalMoney',text:'金额（元）',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
